﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace lcs.Web
{
    /// <summary>
    /// HandlerM 的摘要说明
    /// </summary>
    public class HandlerM : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string keyword = context.Request.QueryString["keyword"];
            if (keyword != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                // 通过JavaScriptSerializer对象的Serialize序列化为["value1","value2",...]的字符串 

                string jsonString = serializer.Serialize(GetFilteredList(keyword, context));
                context.Response.Write(jsonString); // 返回客户端json格式数据  
            }

        }
        public String GetLikeProjectTitle(String keyword)
        {
            String strJson = String.Empty;
            Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
            DataSet ds = bll.GetListname(" projectName like '%" + keyword + "%'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                //strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return strJson;
        }
        private List<lcs.Model.materialbasicinfo> GetFilteredList(string keyword, HttpContext context)
        {

            lcs.BLL.materialbasicinfo bll = new lcs.BLL.materialbasicinfo();
            DataSet ds = bll.GetList(" mms like '%" + keyword + "%'");

            List<lcs.Model.materialbasicinfo> proList = new List<lcs.Model.materialbasicinfo>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                lcs.Model.materialbasicinfo p = new lcs.Model.materialbasicinfo();
                p.mms = ds.Tables[0].Rows[i]["mms"].ToString();                
                //p.projectId = ds.Tables[0].Rows[i]["proid"].ToString();
                //projectId
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                //proList.Add(ds.Tables[0].Rows[i]["projectname"].ToString());
                proList.Add(p);
            }

            return proList;
        }

        private string[] GetFilteredListsname(string sname)
        {
            lcs.BLL.supplierinfo bll = new lcs.BLL.supplierinfo();
            DataSet ds = bll.GetList(" sname like '%" + sname + "%'");

            List<string> proList = new List<string>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                proList.Add(ds.Tables[0].Rows[i]["sname"].ToString());
            }

            return proList.ToArray();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}